package com.sunyur.tailor.channel.service.middle.order.impl;

import com.sunyur.tailor.channel.common.inner.base.MsgResultCode;
import com.sunyur.tailor.channel.common.middle.base.ChannelDataResult;
import com.sunyur.tailor.channel.common.middle.base.ChannelResult;
import com.sunyur.tailor.channel.common.middle.util.ChannelDataResultUtil;
import com.sunyur.tailor.channel.common.middle.util.ChannelResultUtil;
import com.sunyur.tailor.channel.service.inner.order.model.GetOrderHeadModel;
import com.sunyur.tailor.channel.service.inner.order.model.GetOrderListItemModel;
import com.sunyur.tailor.channel.service.inner.order.param.GetOrderHeadParam;
import com.sunyur.tailor.channel.service.inner.order.param.GetOrderListItemParam;
import com.sunyur.tailor.channel.service.inner.order.service.SunyurOrderService;
import com.sunyur.tailor.channel.service.middle.order.MiddleOrderService;
import com.sunyur.tailor.channel.service.outer.workflow.service.OuterWorkflowService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author pengbing
 * @Description 订单服务
 * @Date 19:15 2021/9/6
 * @Param
 * @return
 **/
@Slf4j
@Service("MiddleOrderService")
public class MiddleOrderServiceImpl implements MiddleOrderService {

    @Autowired
    private SunyurOrderService sunyurOrderService;
    @Autowired
    OuterWorkflowService outerWorkflowService;

    @Override
    public ChannelResult auditSubmit(MsgResultCode msgResultCode) throws Exception {
        ChannelDataResult dataResult = getHeadAndItem(msgResultCode);
        if (!ChannelDataResultUtil.isSuccess(dataResult)) {
            return ChannelResultUtil.error(dataResult.getCode(),dataResult.getMessage());
        }
        //业务处理
        GetOrderHeadModel orderHead = (GetOrderHeadModel) dataResult.getHead();
        List<GetOrderListItemModel> orderListItem = (List<GetOrderListItemModel>) dataResult.getItem();

        return ChannelResultUtil.success(true);
    }

    /**
     * 数据实体类转换
     *
     * @param orderHead
     * @param orderListItem
     * @return
     */
    private void conversionAuditSubmit(GetOrderHeadModel orderHead, List<GetOrderListItemModel> orderListItem) {

    }

    @Override
    public ChannelResult invalid(MsgResultCode msgResultCode) throws Exception {
        ChannelDataResult dataResult = getHeadAndItem(msgResultCode);
        if (!ChannelDataResultUtil.isSuccess(dataResult)) {
            return ChannelResultUtil.error(dataResult.getCode(),dataResult.getMessage());
        }
        //业务处理
        GetOrderHeadModel orderHead = (GetOrderHeadModel) dataResult.getHead();
        List<GetOrderListItemModel> orderListItem = (List<GetOrderListItemModel>) dataResult.getItem();

        return ChannelResultUtil.success(true);
    }

    @Override
    public ChannelResult cancel(MsgResultCode msgResultCode) throws Exception {
        ChannelDataResult dataResult = getHeadAndItem(msgResultCode);
        if (!ChannelDataResultUtil.isSuccess(dataResult)) {
            return ChannelResultUtil.error(dataResult.getCode(),dataResult.getMessage());
        }
        //业务处理
        GetOrderHeadModel orderHead = (GetOrderHeadModel) dataResult.getHead();
        List<GetOrderListItemModel> orderListItem = (List<GetOrderListItemModel>) dataResult.getItem();

        return ChannelResultUtil.success(true);
    }

    @Override
    public ChannelResult confirm(MsgResultCode msgResultCode) throws Exception {
        //查询头和行数据
        ChannelDataResult dataResult = getHeadAndItem(msgResultCode);
        if (!ChannelDataResultUtil.isSuccess(dataResult)) {
            return ChannelResultUtil.error(dataResult.getCode(),dataResult.getMessage());
        }
        //业务处理
        GetOrderHeadModel orderHead = (GetOrderHeadModel) dataResult.getHead();
        List<GetOrderListItemModel> orderListItem = (List<GetOrderListItemModel>) dataResult.getItem();

        return ChannelResultUtil.success(true);
    }


    /**
     * @return com.sunyur.tailor.channel.common.middle.base.ChannelResult
     * @Author XuHui
     * @Description 查询头和行信息
     * @Date 13:56 2021/12/28
     * @Param [msgResultCode]
     **/
    private ChannelDataResult getHeadAndItem(MsgResultCode msgResultCode) throws Exception {
        //查询订单头
        GetOrderHeadParam getOrderHeadParam = new GetOrderHeadParam();
        getOrderHeadParam.setOrderCode(msgResultCode.getCode());
        ChannelResult<GetOrderHeadModel> orderHeadResult = sunyurOrderService.getOrderHeadInfo(getOrderHeadParam);
        if (!ChannelResultUtil.isSuccess(orderHeadResult)) {
            return ChannelDataResultUtil.error(orderHeadResult.getCode(), orderHeadResult.getMessage());
        }

        //获取订单行
        GetOrderListItemParam getOrderListItemParam = new GetOrderListItemParam();
        getOrderListItemParam.setOrderCode(msgResultCode.getCode());
        ChannelResult<List<GetOrderListItemModel>> orderItemResult = sunyurOrderService.getOrderListItem(getOrderListItemParam);
        if (!ChannelResultUtil.isSuccess(orderItemResult)) {
            return ChannelDataResultUtil.error(orderItemResult.getCode(), orderItemResult.getMessage());
        }

        return ChannelDataResultUtil.success(orderHeadResult.getContent(),orderItemResult.getContent());
    }
}
